package cycling.taxi.com.ui.activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.OpenClientUtil;
import com.baidu.mapapi.utils.route.BaiduMapRoutePlan;
import com.baidu.mapapi.utils.route.RouteParaOption;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.List;
import cycling.taxi.com.IConstants;
import cycling.taxi.com.R;
import cycling.taxi.com.entity.BusinessCheckpoint;
import cycling.taxi.com.entity.Tips;
import cycling.taxi.com.eventbus.SetTime;
import cycling.taxi.com.request.Config;
import cycling.taxi.com.request.RM;
import cycling.taxi.com.request.RequestBean;
import cycling.taxi.com.request.RequestCallback;
import cycling.taxi.com.request.RequestParams;
import cycling.taxi.com.request.RequestUtil;
import cycling.taxi.com.utils.SPUtil;
import cycling.taxi.com.utils.Urls;

/**
 * 附近检修点列表
 */
public class FixlocationlistActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,RequestCallback,View.OnClickListener{
    private ListView mLocationLv;
    private ListViewAdapter adapter;
    private ArrayList<BusinessCheckpoint>data;
    private LatLng point;
    private ImageView back;
    private TextView title;
    private LatLng ptStart=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fixlocationlist);
        initUI();
        initData();
        mLocationLv.setAdapter(adapter);
    }
    private void initUI() {
        back=(ImageView)findViewById(R.id.back) ;
        mLocationLv = (ListView)findViewById(R.id.mLocationLv);
        title=(TextView)findViewById(R.id.title_text);
        title.setText("附近检修点");
        ptStart=getIntent().getParcelableExtra("ptStart");
        mLocationLv.setOnItemClickListener(this);
        back.setOnClickListener(this);

    }

    private void initData() {
        distance();
    }
    public void distance(){
        RequestParams params = new RequestParams.Builder()
                .build();

        //开始请求发送数据到服务器服务器
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String url = Urls.getFixdistance()+"?sum=2&beanJson="+ SPUtil.getInstance().getData("beanJson").replace(" ","%20");//回去api路径
        RequestUtil.requestData(requestQueue,
                IConstants.FIXDISTANCE,
                url,
                RM.GET,
                RequestBean.class,
                params,
                this);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Adapter adpter=parent.getAdapter();
        BusinessCheckpoint item=(BusinessCheckpoint) adpter.getItem(position);//拿到当前数据值并强转   adpter.getItem(i)即为当前数据对象
        String gps=item.getGps();
        String gpssplit[]=gps.split(",");
        point=new LatLng(Double.parseDouble(gpssplit[1]),Double.parseDouble(gpssplit[0]));
        Intent itemIntent= new Intent(this, FixlocationlistsActivity.class);
        itemIntent.putExtra("LatLng",point);
        itemIntent.putExtra("Item",item);
        itemIntent.putExtra("ptStart",ptStart);
        startActivity(itemIntent);
    }
    @Override
    public void requestSuccess(RequestBean bean, Config config) {
        data = new Gson().fromJson(bean.beanJson,new TypeToken<List<BusinessCheckpoint>>(){}.getType());
        adapter = new ListViewAdapter();
        mLocationLv.setAdapter(adapter);
    }

    @Override
    public void requestFail(RequestBean bean, Config config) {

    }

    @Override
    public void requestTokenInvalid(String requestTag, Config config) {

    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.back){
            finish();
        }
    }

    public class ListViewAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder=null;
            if (convertView==null) {
                convertView = View.inflate(FixlocationlistActivity.this,R.layout.fixlocationlistitem, null);
            }
            BusinessCheckpoint businessCheckpoint=data.get(position);
            holder = new ViewHolder();
            holder.tv = (TextView) convertView.findViewById(R.id.lv_location);
            holder.name=(TextView) convertView.findViewById(R.id.name);
            holder.distance=(TextView)convertView.findViewById(R.id.distance);
            holder.tv.setText(businessCheckpoint.getAddress());
            holder.name.setText(businessCheckpoint.getName());
            holder.distance.setText(String.valueOf(Double.parseDouble(businessCheckpoint.getDistance())/10));
            String gps=businessCheckpoint.getGps();
            String gpssplit[]=gps.split(",");
            point=new LatLng(Double.parseDouble(gpssplit[1]),Double.parseDouble(gpssplit[0]));
            convertView.setTag(holder);

            return convertView;
        }
    }

    static class ViewHolder {
        TextView tv;
        TextView name;
        TextView distance;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();

    }



}
